#include "FKLogger.h"

#include <QDataStream>
#include "FKSystemObject.h"

FKFactory<FKSystemObject>* FKSystemObject::_factory=new FKFactory<FKSystemObject>;

FKSystemObject::FKSystemObject():QObject(){
    FK_CBEGIN
    FK_CEND
}

FKSystemObject::~FKSystemObject(){
    FK_DBEGIN
    FK_DEND
}

void FKSystemObject::pack(QDataStream &stream){
    Q_UNUSED(stream)
}

bool FKSystemObject::load(QDataStream &stream){
    Q_UNUSED(stream)
    return true;
}

FKSystemObject* FKSystemObject::createObject(QString className){
    return _factory->createObject(className);
}

FKSystemObject* FKSystemObject::createObject(QString className, qint32 id){
    return _factory->createObject(className,id);
}

void FKSystemObject::packObject(QDataStream &stream){
    QString className(metaObject()->className());
    qint32 id=-1;
    stream<<className;
    stream<<id;
    pack(stream);
    stream<<className;
}

FKSystemObject* FKSystemObject::loadObject(QDataStream &stream){
    QString className, endTag;
    qint32 id;
    stream>>className;
    stream>>id;
    FKSystemObject* obj=createObject(className,id);
    if(obj){
        if(obj->load(stream)){
            stream>>endTag;
            if(className!=endTag){
                obj->deleteLater();
                obj=0;
            }
        }else{
            obj->deleteLater();
            obj=0;
        }
    }
    return obj;
}
